Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Adds a guide to writing code #751

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

a-ovchinnikov
Copy link
Collaborator

This commit adds a reference document with some
best practices to help resolve disputes when
writing code.

Related to: #737

Maintainers will complete the following section

  • Commit messages are descriptive enough
  • Code coverage from testing does not decrease and new code is covered
  • Docs updated (if applicable)
  • Docs links in the code are still valid (if docs were updated)

Note: if the contribution is external (not from an organization member), the CI
pipeline will not run automatically. After verifying that the CI is safe to run:

@slimreaper35
Copy link
Member

Shouldn't be this part of CONTRIBUTING.md ?

@MartinBasti
Copy link
Contributor

Shouldn't be this part of CONTRIBUTING.md ?

Should be linked there at least

@slimreaper35
Copy link
Member

I would not mix docs for package managers and coding best practices 🤷

@eskultety
Copy link
Member

Shouldn't be this part of CONTRIBUTING.md ?

Ideally not. Contributing guidelines are already quite bloated in content, so stylistic things should go to something like "HACKING.md"

Shouldn't be this part of CONTRIBUTING.md ?

Should be linked there at least

Yes, agreed.


## FAQ

Q. Is this guide exhaustive?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
This doesn't render well

@a-ovchinnikov a-ovchinnikov force-pushed the python_coding_guide branch 3 times, most recently from 37e2b22 to 7a4e3be Compare November 29, 2024 16:42
@a-ovchinnikov a-ovchinnikov marked this pull request as ready for review December 3, 2024 14:50

### Write easy to follow code

- Try avoiding extremely long lines in any direction. Horizontal lines longer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a linter rule that covers this. Is it necessary to add this point here as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Horizontal -- yes, vertical -- not really. I'd have kept it for completeness.

- Consider variable names which reflect intended use and potentially types of
objects they will be bound with.

- While doing the above remember, that vowel shortage is over, so in most
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I completely get what you mean here. Can you add a quick example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lst_pckgs and other overly compacted names. This is not a problem in the codebase yet and arguably I am the one most prone to using them (only in very narrow scopes, but nevertheless).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Vowel shortage" made me chuckle; very much felt.

* [Write clear tests which actually test functionality](#write-clear-tests-which-actually-test-functionality)
* [FAQ](#faq)

## Background
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Altough I think this section is very well written and is pertinent, I'm afraid that the large text might scare off some people from reading. I feel like the two first paragraphs have the most important messages (we enforce good standards for maintainability, and reviews are done in good faith).

In any case, I'm not strongly against keeping this section as is, so please do so if you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was taking a wider look at both CONTRIBUTING.md and coding_best_practices.md, and they might be a little too much information for a newcomer. Maybe we can add some quick reference summaries later on, so one can get up to speed quickly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intended to be more of a supplementary text and not a must-read. All I do here is reiterate basics. I'll reword the referring sentence in CONTRIBUTING.md to highlight this.

and [PEP20](https://peps.python.org/pep-0020);


### Write clear tests which actually test functionality
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we drop the test guidelines from CONTRIBUTING.md?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. That guide is the main one, this is supplementary.


## Best practices

### Document your decisions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we drop the comment guidelines from CONTRIBUTING.md?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no. The idea is that CONTRIBUTING.md is enough for most if not all contributors, and this is a fall-back for cases when we need to explain why certain piece of code does not pass a code review.

@a-ovchinnikov a-ovchinnikov force-pushed the python_coding_guide branch 2 times, most recently from e8c0973 to a82a3e6 Compare December 12, 2024 20:06
This commit adds a reference document with some
best practices to help resolve disputes when
writing code.

Related to: containerbuildsystem#737

Signed-off-by: Alexey Ovchinnikov <[email protected]>
@ben-alkov
Copy link
Member

I really like what's here so far (not least because I strongly agree with all of it 🤣).

Have you considered adding a link to The Zen of Python? I don't know how well-known it is these days, but IMO it's an invaluable resource (outside of Python, even).

@a-ovchinnikov
Copy link
Collaborator Author

a-ovchinnikov commented Dec 17, 2024

Have you considered adding a link to The Zen of Python?

Yes, I have provided a link to 19 of the 20 postulates. Edit: L201, GH keeps rendering the page with the highlight just off screen.

@ben-alkov
Copy link
Member

Have you considered adding a link to The Zen of Python?

Yes, I have provided a link

LOL, I had forgotten that it started life as a PEP 😆

@slimreaper35
Copy link
Member

My suggestion would be this:

  1. Put some general paragraphs that are valid into CONTRIBUTING.md (it is a standardized document that is inside the root of the repository, it is more visible and contributors might read it).

  2. There is NO way anybody will ever remember what is inside this document or maintain it (other user-facing docs about package managers are barely touched).

  3. People have/prefer different programming styles, and we should simply just accept that. You can have multiple solutions to the same problem.

  4. If we want to be really strict about something, just find the rule in one of many linters and apply it. I've seen here mentions of line length, or function length...

@a-ovchinnikov
Copy link
Collaborator Author

@slimreaper35 thank you for the suggestions! Please let me start with quoting the newly added document:

this text is supplementary and contains a condensation of well-known best
practices

To reiterate, this is not something new, I would expect that most if not all future contributors are well familiar with these concepts. The document is related to #737 and to a discussion that caused a submission of the aforementioned issue. The reason for this document is essentially the same as for a Code of Conduct: to have the obvious spelled out for the rare case when it is not obvious.

Put some general paragraphs that are valid into CONTRIBUTING.md (it is a
standardized document that is inside the root of the repository, it is more
visible and contributors might read it).

The document is linked to from CONTRIBUTING.md, is supplementary, is hopefully clearly marked as one, and does not contain any unique knowledge. This is a condensation of best practices known to mankind for at least thirty years (likely more, I arbitrarily count from GoF publication year to have some common reference point).

There is NO way anybody will ever remember what is inside this document or
maintain it (other user-facing docs about package managers are barely
touched).

There is no need to (see the points above, below, and also this fragment from the document itself: "It is ... intended ... to be used as a reference and a disambiguation tool for code reviewers").

People have/prefer different programming styles, and we should simply just
accept that. You can have multiple solutions to the same problem.

This document deals with the ways of communicating intent and not with styles. In other words the document explains how to be comprehensible. While there is an infinite number of pieces of code that a) compile and b) solve some problem only a small subset of those are comprehensible and, by induction maintainable. No one can maintain a bit of code that they cannot comprehend. Maintaining a bit of code that is hard to comprehend is not economically feasible as well. Assembly is a fine language and it is absolutely possible to write just in it if one is ready to pay premium for bug fixes and feature requests. (For another graphic example see code snippets in the "Background" section).

Issue 737 arose from an argument about maintainability, one of complaints during the argument was about a lack of document with basics of clear coding practices. This PR adds such document. Please note, that this is not an original research: a multitude of books, conference talks, and blogposts have been created by others which address the same problems and recommend the same approaches in a more verbose and argumented way. This knowledge is so common that I did not even feel the need to trace any of the statements to their sources and did not include any references. I believe it is abundantly clear that I am not the original author of any of these suggestions and merely act as a scribe. Since this is already a preexisting knowledge, the document simply provides a common ground for rare cases when it is not.

If we want to be really strict about something, just find the rule in one of
many linters and apply it. I've seen here mentions of line length, or
function length...

Linters are not context-aware. The bulk of the document is concerned with logical organization and reasoning about why certain patterns work and why others do not (this especially applies to the parts about lengths and alignments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants